package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.BuyOrderBean;
import com.project.bean.ProductBean;
import com.project.bean.SaleOrderBean;
import com.project.bean.UserBean;
import com.project.service.IProductService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.processing.Filer;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 商品controller
 */
@RequestMapping("product")
@RestController
public class ProductController {

    @Autowired
    private IProductService productService;

    //添加商品
    @RequestMapping("add")
    public String add(ProductBean product, Integer num, HttpServletRequest request,MultipartFile face) throws IOException {
        System.err.println(product);
        if (face!=null){//判断用户是否有文件上传
            //得到文件上传名称
            String filename = face.getOriginalFilename();
            File dir = new File("D:\\code\\invoicing\\invoicingMyBatisPlus\\src\\main\\resources\\productImg");
            if (!dir.exists()){
                dir.mkdirs();
            }
            //重命名文件，防止上传文件名相同导致的覆盖
            filename = System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
            product.setImgPath(filename);

            //将上传文件的二进制数据，保存在服务器本地文件
            face.transferTo(new File("D:\\code\\invoicing\\invoicingMyBatisPlus\\src\\main\\resources\\productImg"+filename));
        }
        UserBean loginUser = (UserBean) request.getSession().getAttribute("loginUser");
        productService.add(product,num, loginUser.getId());
        return "ok";
    }

    //动态分页查询商品
    @RequestMapping("findByItem")
    public IPage<ProductBean> findByItem(Integer pageNO, String name, Integer startNum,
                                         Integer endNum, String salePrice, String type){
        return productService.findByItem(pageNO, name, startNum, endNum, salePrice, type);
    }

    //通过库存商品id查询库存商品对象
    @RequestMapping("findById")
    public ProductBean findById(Integer id){
        return productService.findById(id);
    }

    //通过库存id修改销售价格
    @RequestMapping("updateSalePrice")
    public String updateSalePrice(Integer id, Integer newSalePrice){
        productService.updateSalePrice(id, newSalePrice);
        return "ok";
    }

    //入库
    @RequestMapping("inKu")
    public String inKu(HttpServletRequest request,Integer vendorId){
        UserBean loginUser = (UserBean)request.getSession().getAttribute("loginUser");
        productService.inKu(new BuyOrderBean(vendorId,loginUser.getId()));
        return "ok";
    }

    //出库
    @RequestMapping("outKu")
    public String outKu(SaleOrderBean orderBean){
        productService.outKu(orderBean);
        return "ok";
    }
}
